/*
 * Copyright 2012 ITC Tsinghua University. All Rights Reserved.
 */
package cn.edu.tsinghua.cic.ccss.task;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.forten.dic.util.EhcacheHelper;
import org.forten.utils.system.LogUtil;
import org.springframework.stereotype.Component;

/**
 * 
 * 
 * @author <a href="mailto:du_yi@bbn.cn">Duyi</a>
 * 
 * @since 2012-6-21
 */
@Component("ehcacheUtil")
public class EhcacheUtil {
	private static Logger log = Logger.getLogger(EhcacheUtil.class);
	private static final String INIT_MSG = "初始化定时任务执行标记完成";

	@Resource
	private EhcacheHelper ehcacheHelper;

	@PostConstruct
	public void initTaskFlag() {
		ehcacheHelper.writeToCache("taskExecuteFlagCache", "creditFlag", 0);
		ehcacheHelper.writeToCache("taskExecuteFlagCache", "mailFlag", 0);
		LogUtil.info(log, INIT_MSG);
		getFlag("creditFlag");
		getFlag("mailFlag");
	}

	public int getFlag(String key) {
		int flag = (Integer) ehcacheHelper.getValue("taskExecuteFlagCache", key);
		LogUtil.debug(log, "taskExecuteFlagCache：key[%s],value[%d]", key,flag);
		return flag;
	}

	public void setFlag(String key, int value) {
		ehcacheHelper.writeToCache("taskExecuteFlagCache", key, value);
	}
}
